<template>

    <div class="todo-page-header">
        <div class="header-breadcrumbs">
            <a class="breadcrumb-item" @click="toProject">{{projectName?projectName:'暂无'}}</a>
        </div>


        <div class="header-actions">

            <v-tooltip bottom>
                <template v-slot:activator="{ on, attrs }">
                    <a class="link-team-wide-id" @click="copyLink()" v-bind="attrs" v-on="on">
                        <span class="original-text"># {{task.id?task.id:''}}</span>
                    </a>
                </template>
                <span>复制链接</span>
            </v-tooltip>

            <v-tooltip bottom>
                <template v-slot:activator="{ on, attrs }">
                    <v-btn @click="startTask()" icon v-show="task?(task.status==='wait'):false" v-bind="attrs" v-on="on">
                        <v-icon color="grey lighten-1">mdi-play-circle</v-icon>
                    </v-btn>
                </template>
                <span>开始任务</span>
            </v-tooltip>

            <v-tooltip bottom>
                <template v-slot:activator="{ on, attrs }">
                    <v-btn @click="pauseTask()" icon v-show="task?task.status==='doing':false" v-bind="attrs" v-on="on">
                        <v-icon color="green lighten-1">mdi-pause-circle</v-icon>
                    </v-btn>
                </template>
                <span>暂停任务</span>
            </v-tooltip>

            <v-tooltip bottom>
                <template v-slot:activator="{ on, attrs }">
                    <v-btn @click="startTask()" icon v-show="task?(task.status==='pause'):false" v-bind="attrs" v-on="on">
                        <v-icon color="grey lighten-1">mdi-play-circle</v-icon>
                    </v-btn>
                </template>
                <span>继续任务</span>
            </v-tooltip>

            <!--                已收藏-->
            <v-tooltip bottom>
                <template v-slot:activator="{ on, attrs }">
                    <v-btn @click.stop="cancelStar()" icon v-show="task?(task.isfavorites==='1'):false" v-bind="attrs" v-on="on">
                        <v-icon color="lime lighten-3">mdi-star</v-icon>
                    </v-btn>
                </template>
                <span>取消收藏</span>
            </v-tooltip>
            <!--                未收藏-->
            <v-tooltip bottom>
                <template v-slot:activator="{ on, attrs }">
                    <v-btn @click.stop="onStar()" icon v-show="task?(task.isfavorites==='0'):false" v-bind="attrs" v-on="on">
                        <v-icon color="grey lighten-1">mdi-star</v-icon>
                    </v-btn>
                </template>
                <span>收藏</span>
            </v-tooltip>


            <el-dropdown trigger="click" @command="handleCommand">
                      <span class="el-dropdown-link">
                          <v-btn icon><v-icon>mdi-dots-horizontal</v-icon></v-btn>
                      </span>
                <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="close" v-show="task?!(task.status==='cancel' || task.status==='closed' ):false" >关闭</el-dropdown-item>
                    <el-dropdown-item command="cancel" v-show="task?!(task.status==='cancel' || task.status==='closed' ):false" >取消</el-dropdown-item>
                    <el-dropdown-item command="activate" v-show="task?(task.status==='cancel' || task.status==='closed' ):false" >激活</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>

        </div>

    </div>


</template>

<script lang="ts">
    import { Vue, Component, Prop, Model } from 'vue-property-decorator';
    import TaskService from "@service/task/task-service";
    import {Environment} from "@/environments/environment";

    @Component({})
    export default class appTaskpageProjectheader extends Vue {

        /**
         * 实体服务对象
         *
         * @type {TaskService}
         * @memberof TaskEditViewBase
         */
        public appEntityService: TaskService = new TaskService;


        /**
         * 视图参数
         *
         * @type {*}
         * @memberof AppRichTextEditor
         */
        @Prop() public viewparams!: any;

        /**
         * 视图上下文
         *
         * @type {*}
         * @memberof AppRichTextEditor
         */
        @Prop() public context!: any;

        /**
         * 项目名称
         */
        public projectName: string = '';

        /**
         * 项目id
         */
        public projectId: string = '';

        /**
         * 任务数据
         */
        public task: any = {};



        /**
         * 跳转到项目页面
         */
        public toProject() {
            let viewparam: any = {};
            const deResParameters: any = [];
            const parameters: any = [
                {pathName: "projects", parameterName: "project"},
                {pathName: "infodashboardview", parameterName: "infodashboardview"},
            ];
            let args = [{project: this.projectId}];
            const path: any = this.$viewTool.buildUpRoutePath(
                this.$route,
                {},
                deResParameters,
                parameters,
                args,
                viewparam
            );
            this.$router.push(path);
        }


        /**
         * 点击开始任务
         */
        public startTask() {
            let context: any = {task: this.task.id};
            /*ACTION_PARAMS_START.put("realStarted", "0000-00-00");	实际开始    日期型
            ACTION_PARAMS_START.put("consumed", null); 总计消耗  Double
            ACTION_PARAMS_START.put("left", null);  预计剩余  Double
            ACTION_PARAMS_START.put("comment", ""); 备注  String*/
            let params: any = {realStarted: this.getNowFormatDate2(), consumed: 8, left: 8, comment: ''};
            let data = {};
            Object.assign(data, params);
            let isloading = true;
            return new Promise((resolve: any, reject: any) => {
                let result: Promise<any>;
                result = this.appEntityService.Start(context, data, isloading);
                result.then((response) => {
                    console.log(response);
                    if (response.data) {
                        // this.task.status = 'doing';
                        this.$emit('getActionData', this.task.id);
                        this.searchTaskByTaskId(this.task.id);
                    }
                }).catch(response => {
                    if(response && response.status === 500){
                        const url: any = '/500';
                        this.$router.push({path: url});
                    }else{
                        this.$Notice.error({title: '错误', desc: response.data.detail});
                        reject(response);
                    }
                });
            });
        }

        /**
         * 点击暂停任务
         */
        public pauseTask() {
            let context: any = {task: this.task.id};
            // ACTION_PARAMS_PAUSE.put("comment", ""); 备注 string
            let params: any = {comment: ''};
            let data = {};
            Object.assign(data, params);
            let isloading = true;
            return new Promise((resolve: any, reject: any) => {
                let result: Promise<any>;
                result = this.appEntityService.Pause(context, data, isloading);
                result.then((response) => {
                    if (response.data) {
                        // this.task.status = 'pause';
                        this.$emit('getActionData', this.task.id);
                        this.searchTaskByTaskId(this.task.id);
                    }
                }).catch(response => {
                    if(response && response.status === 500){
                        const url: any = '/500';
                        this.$router.push({path: url});
                    }else{
                        this.$Notice.error({title: '错误', desc: response.data.detail});
                        reject(response);
                    }
                });
            });
        }

        /**
         * 收藏
         */
        public onStar() {
            let context: any = {task: this.task.id};
            let params: any = {
                id: this.task.id,
                name: this.task.name,
                srfparentdename: "Task",
                srfparentkey: this.task.id
            };
            let data = {};
            Object.assign(data, params);
            let isloading = true;
            return new Promise((resolve: any, reject: any) => {
                let result: Promise<any>;
                result = this.appEntityService.TaskFavorites(context, data, isloading);
                result.then((response) => {
                    if (response.data) {
                        this.$emit('getActionData', this.task.id);
                        this.searchTaskByTaskId(this.task.id);
                    }
                }).catch(response => {
                    if(response && response.status === 500){
                        const url: any = '/500';
                        this.$router.push({path: url});
                    }else{
                        this.$Notice.error({title: '错误', desc: response.data.detail});
                        reject(response);
                    }
                });
            });
        }

        /**
         * 取消收藏
         */
        public cancelStar() {
            let context: any = {task: this.task.id};
            let params: any = {
                id: this.task.id,
                name: this.task.name,
                srfparentdename: "Task",
                srfparentkey: this.task.id
            };
            let data = {};
            Object.assign(data, params);
            let isloading = true;
            return new Promise((resolve: any, reject: any) => {
                let result: Promise<any>;
                result = this.appEntityService.TaskNFavorites(context, data, isloading);
                result.then((response) => {
                    if (response.data) {
                        this.$emit('getActionData', this.task.id);
                        this.searchTaskByTaskId(this.task.id);
                    }
                }).catch(response => {
                    if(response && response.status === 500){
                        const url: any = '/500';
                        this.$router.push({path: url});
                    }else{
                        this.$Notice.error({title: '错误', desc: response.data.detail});
                        reject(response);
                    }
                });
            });
        }


        /**
         * 声明周期
         */
        public created() {
            let taskId = this.context.task;
            if(!taskId){
                taskId = this.viewparams.taskId;
            }
            if (taskId) {
                // 查询任务信息
                this.searchTaskByTaskId(taskId);

            }
        }

        /**
         * 项目请求路径
         */
        public taskUrl: string = "tasks/";

        /**
         * 查找任务
         * @param taskId
         */
        public searchTaskByTaskId(taskId: any) {
            return new Promise((resolve: any, reject: any) => {
                let result: Promise<any>;
                result = this.appEntityService.searchTaskByTaskId(this.taskUrl + taskId, {}, true);
                result.then((response) => {
                    if (response && response.status === 200) {
                        if (response.data && response.data.id) {
                            if (JSON.stringify(response.data) && JSON.stringify(response.data).length > 0) {
                                this.projectName = response.data.projectname;
                                this.projectId = response.data.project;
                                this.task = response.data;
                                this.$forceUpdate();
                            }
                        } else {
                            // const url: any = '/404';
                            // this.$router.push({path: url});
                            this.$Notice.error({title: '提示', desc: '该任务不存在，没有这个任务！'});
                        }
                    }
                }).catch(response => {
                    // console.log(fetch);
                    if(response && response.status === 500){
                        const url: any = '/500';
                        this.$router.push({path: url});
                    }else{
                        this.$Notice.error({title: '错误', desc: response.data.detail});
                        reject(response);
                    }
                });
            });

        }


        /**
         * 下拉点击
         * 最右上角的三个点
         * @param command 命令
         * @param ell 点击的元素
         */
        public handleCommand(command: any, ell: any) {
            // this.$message('click on item ' + command);
            // 取消任务
            if(command == 'cancel'){
                this.cancelTask();
            }
            // 关闭任务
            else if(command == 'close'){
                this.closeTask();
            }
            // 激活任务
            else if(command == 'activate'){
                this.activateTask();
            }
        }

        /**
         * 取消任务
         */
        public cancelTask(){
            let context: any = {task: this.task.id};
            // ACTION_PARAMS_PAUSE.put("comment", ""); 备注 string
            let params: any = {comment: ''};
            let data = {};
            Object.assign(data, params);
            let isloading = true;
            return new Promise((resolve: any, reject: any) => {
                let result: Promise<any>;
                result = this.appEntityService.Cancel(context, data, isloading);
                result.then((response) => {
                    if (response.data) {
                        // this.task.status = 'pause';
                        this.$emit('getActionData', this.task.id);
                        this.$emit('refreshDetail', this.task.id);
                        this.searchTaskByTaskId(this.task.id);
                    }
                }).catch(response => {
                    if(response && response.status === 500){
                        const url: any = '/500';
                        this.$router.push({path: url});
                    }else{
                        this.$Notice.error({title: '错误', desc: response.data.detail});
                        reject(response);
                    }
                });
            });
        }

        /**
         * 关闭任务
         */
        public closeTask(){
            let context: any = {task: this.task.id};
            // ACTION_PARAMS_PAUSE.put("comment", ""); 备注 string
            let params: any = {comment: ''};
            let data = {};
            Object.assign(data, params);
            let isloading = true;
            return new Promise((resolve: any, reject: any) => {
                let result: Promise<any>;
                result = this.appEntityService.Close(context, data, isloading);
                result.then((response) => {
                    if (response.data) {
                        // this.task.status = 'pause';
                        this.$emit('getActionData', this.task.id);
                        this.$emit('refreshDetail', this.task.id);
                        this.searchTaskByTaskId(this.task.id);
                    }
                }).catch(response => {
                    if(response && response.status === 500){
                        const url: any = '/500';
                        this.$router.push({path: url});
                    }else{
                        this.$Notice.error({title: '错误', desc: response.data.detail});
                        reject(response);
                    }
                });
            });
        }

        /**
         * 激活任务
         */
        public activateTask(){
            let context: any = {task: this.task.id};
            // ACTION_PARAMS_ACTIVATE.put("assignedTo", null);
            // ACTION_PARAMS_ACTIVATE.put("left", null);
            // ACTION_PARAMS_ACTIVATE.put("comment", "");
            let params: any = {id: this.task.id, left: 5, assignedto: this.task.assignedto, comment: ''};
            let data = {};
            Object.assign(data, params);
            let isloading = true;
            return new Promise((resolve: any, reject: any) => {
                let result: Promise<any>;
                result = this.appEntityService.Activate(context, data, isloading);
                result.then((response) => {
                    if (response.data) {
                        // this.task.status = 'pause';
                        this.$emit('getActionData', this.task.id);
                        this.$emit('refreshDetail', this.task.id);
                        this.searchTaskByTaskId(this.task.id);
                    }
                }).catch(response => {
                    if(response && response.status === 500){
                        const url: any = '/500';
                        this.$router.push({path: url});
                    }else{
                        this.$Notice.error({title: '错误', desc: response.data.detail});
                        reject(response);
                    }
                });
            });
        }

        /**
         * 复制当前任务链接
         */
        public copyLink(){
            let Url2 = window.location.href;
            let oInput = document.createElement("input");
            oInput.value = Url2;
            document.body.appendChild(oInput);
            oInput.select(); // 选择对象
            document.execCommand("Copy"); // 执行浏览器复制命令
            oInput.className = "oInput";
            oInput.style.display = "none";
            this.$message({message:"复制成功!", type:'info'})
        }


        /**
         * 获取时间 yyyy-MM-dd mm:hh:ss
         */
        public getNowFormatDate() {
            let date = new Date();
            let seperator1 = "-";
            let seperator2 = ":";
            let month: any = date.getMonth() + 1;
            let strDate: any = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            let currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                + " " + date.getHours() + seperator2 + date.getMinutes()
                + seperator2 + date.getSeconds();
            return currentdate;
        }

        /**
         * 获取时间 yyyy-MM-dd
         */
        public getNowFormatDate2() {
            let date = new Date();
            let seperator1 = "-";
            let seperator2 = ":";
            let month: any = date.getMonth() + 1;
            let strDate: any = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            return date.getFullYear() + seperator1 + month + seperator1 + strDate;
        }




    }
</script>

<style lang='less'>
    @import './app-taskpage-projectheader.less';
</style>